package ObserverMode.JavaUtilsEdition.observer;

import ObserverMode.InitialEdition.observer.DisplayElement;
import ObserverMode.JavaUtilsEdition.subject.WeatherData2;

import java.util.Observable;
import java.util.Observer;

/**
 * 重做观察者
 *
 * @Author Li Cheng
 * @Date 2021/8/12 22:57
 * @Version 1.0
 */
public class CurrentConditionsDisplay2 implements Observer , DisplayElement {
    Observable observable;
    private float temperature;
    private float humidity;

    //需要observable当参数，使之成为观察者
    public CurrentConditionsDisplay2(Observable observable){
        this.observable = observable;
        observable.addObserver(this);
    }


    @Override
    public void display() {
        System.out.println("Current conditions: " + temperature + " F degrees and "+ humidity + "% humidity");
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData2){
            WeatherData2 weatherData2 = (WeatherData2) o;
            this.temperature = weatherData2.getTemperature();
            this.humidity = weatherData2.getHumidity();
            display();
        }
    }
}
